//声明一个函数
const countReducer = (state=100, action) => {
  //根据 action 操作对象, 对 state 数据进行处理, 然后返回新的状态值
  switch(action.type){
    case 'jia':
      return state + action.payload;
    case 'jian':
      return state - action.payload;
    default: 
      //这行代码必须要加, 通过 state 的值, 设置内部状态的值
      return state;
  }
}

export default countReducer;

//封装 action creator action对象的创造器
export function jiaAction(payload){
  return {type: 'jia', payload: payload }
}

export function jianAction(payload){
  return {type: 'jian', payload: payload};
}

//异步更新状态
export function asyncJiaAction(payload){
  return dispatch => {
    setTimeout(() => {
      dispatch({type: 'jia', payload: payload})
    }, 1000);
  }
}
